﻿using CommonTool;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

/// <summary>
/// 串口扫码类
/// </summary>
namespace Tool
{
  public  class SerialPortDemo
    {

        /// <summary>
        /// 创建一个串口操作对象
        /// </summary>
        private SerialPort serialPort = null;
        public SerialPort SerialPortObject
        {
            get { return serialPort; }
        }


        /// <summary>
        /// 获取或设置串口的串口号
        /// </summary>
        public string PortName { get; set; }

        /// <summary>
        /// 设置波特率
        /// </summary>
        public int BaudRate { get; set; }

        /// <summary>
        /// 获取或设置串口的校验位
        /// </summary>
        public Parity Parity { get; set; }

        /// <summary>
        /// 获取或设置串口的数据位
        /// </summary>
        public int DataBits { get; set; }

        /// <summary>
        /// 获取或设置串口的停止位
        /// </summary>
        public StopBits StopBits { get; set; }

        /// <summary>
        /// 获取或设置串口的数据
        /// </summary>
        public  string  SerialData { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="portName">串口号</param>
        /// <param name="baudRate">波特率</param>
        /// <param name="dataBits"><数据位/param>
        /// <param name="stopBits">停止位</param>
        /// <param name="parity">校验位</param>
        public SerialPortDemo(string portName, int baudRate, int dataBits,StopBits stopBits,Parity parity)
        {
            this.serialPort = new SerialPort();

            this.serialPort.PortName = portName;
            this.serialPort.BaudRate = baudRate;
            this.serialPort.Parity = parity;
            this.serialPort.DataBits = dataBits;
            this.serialPort.StopBits = stopBits;         
        }

        /// <summary>
        /// 打开串口
        /// </summary>
        public void OpenPort()
        {
            try
            {
                if (!serialPort.IsOpen)
                {
                    this.serialPort.NewLine = "\r\n";
                    this.serialPort.RtsEnable = false;
                    this.serialPort.Open();
                   // this.serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
                }
            }
            catch (Exception ex)
            {
                LogView.GetInstance().ShowLogSave(LogView.LogType.Error, ex.Message);//保存Log
                MessageBox.Show(ex.Message);
            }

        }

        /// <summary>
        /// 关闭串口
        /// </summary>
        public void ClosePort()
        {
            try
            {
                if (this.serialPort.IsOpen)
                {
                    this.serialPort.Close();
                }
            }
            catch (Exception ex )
            {
                LogView.GetInstance().ShowLogSave(LogView.LogType.Error, ex.Message);//保存Log
                MessageBox.Show(ex.Message);
            }

        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="data"></param>
        public void Send(string data)
        {
            try
            {
                if (this.serialPort.IsOpen)
                {
                    //  this.serialPort.Write(data);
                    this.serialPort.WriteLine(data);
                }
            }
            catch (Exception ex )
            {
                LogView.GetInstance().ShowLogSave(LogView.LogType.Error, ex.Message);//保存Log
                MessageBox.Show(ex.Message);
            }

        }

        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                if (this.serialPort.IsOpen)
                {
                    // SerialData = this.serialPort.ReadTo("\r");
                    SerialData = this.serialPort.ReadExisting();
                }
            }
            catch (Exception ex)
            {
                LogView.GetInstance().ShowLogSave(LogView.LogType.Error, ex.Message);//保存Log
                MessageBox.Show(ex.Message);
            }

        }

        

        public  bool Status()
        {
            if (this.serialPort.IsOpen == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
